package com.client;

import java.util.ArrayList;
import java.util.List;

import com.client.data.AssureListGrid;
import com.client.data.PropositionListGrid;
import com.client.service.data.AssureDataService;
import com.client.service.data.AssureDataServiceAsync;
import com.client.service.rule.DecisionMakerService;
import com.client.service.rule.DecisionMakerServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.shared.data.TypeIData;
import com.shared.model.Assure;
import com.shared.model.Proposition;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;

public class PanelBaseClient extends Composite {

	private final AssureDataServiceAsync assureService = GWT.create(AssureDataService.class);
	private final DecisionMakerServiceAsync decisionService = GWT.create(DecisionMakerService.class);
	
	List<Assure> listeAssure = new ArrayList<Assure>();
	AssureListGrid listGrid = new AssureListGrid();
	private int indexSelected = 0;
	
	PropositionListGrid propositionListGrid = new PropositionListGrid();
	
		
	ListBox decisionListBox = new ListBox();
	Button decisionButton = new Button("Appliquer règle");
	
	public PanelBaseClient(){
		
		initWidget(createPanelBaseClient());
		
		listGrid.setSelectionType(SelectionStyle.MULTIPLE);
		
		assureService.getList(new AsyncCallback<List>() {			
				
			public void onSuccess(List result) {					
				listeAssure = result ;	
				fillGrid();
			}
			public void onFailure(Throwable caught) {				
				Window.alert(caught.getMessage());				
			}
	
		});
		
				
		listGrid.addCellClickHandler(new CellClickHandler() {
			
			public void onCellClick(CellClickEvent event) {
				indexSelected = event.getRowNum();
				fillGridProposition();
				
			}
		});
		
		decisionService.getAvailableDM(new AsyncCallback<String[]>() {			
			public void onSuccess(String[] result) {
				for(String s : result)	decisionListBox.addItem(s);				
			}			
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());				
			}
		});
		
		decisionButton.addClickHandler(new ClickHandler() {		
					
			public void onClick(ClickEvent event) {
					
					if(listGrid.getSelection().length==0){
						Window.alert("Vous devez sélectionner au moins un assuré");
					}else{
						for(int i : listGrid.getSelection(listGrid.getSelection())){
							decisionService.operate(decisionListBox.getItemText(decisionListBox.getSelectedIndex()),
									listeAssure.get(i), TypeIData.ASSURE, new AsyncCallback<String>() {									
										public void onSuccess(String result) {	
											
										}									
										public void onFailure(Throwable caught) {										
											Window.alert(caught.getMessage());											
										}
									});
						}
						fillGridProposition();
					}
			}
		});
		
		
	}
	
	public VerticalPanel createPanelBaseClient(){
		
		final VerticalPanel panelBaseClient = new VerticalPanel();
		panelBaseClient.setSpacing(10);
		panelBaseClient.add(listGrid);
		
		
		
		
		HorizontalPanel decisionPanel = new HorizontalPanel();
		decisionPanel.setSpacing(5);
		decisionPanel.add(decisionListBox);
		decisionPanel.add(decisionButton);
		
		panelBaseClient.add(decisionPanel);
		
		Label labelPropo = new Label("Propositions : ");
		labelPropo.setStyleName("titre");
		panelBaseClient.add(labelPropo);
		panelBaseClient.add(propositionListGrid);
		propositionListGrid.setVisible(false);
		return(panelBaseClient);
	}
	
	public void fillGrid(){
		for(Assure p : listeAssure){			
			listGrid.addData(p);			
		}
	}
	
	public void fillGridProposition(){
		propositionListGrid.selectAllRecords();
		propositionListGrid.removeSelectedData();
		
		assureService.getListProposition(listeAssure.get(indexSelected), new AsyncCallback<List>() {
			
			public void onSuccess(List result) {
				for(Proposition p : (List<Proposition>) result) propositionListGrid.addData(p);	
				propositionListGrid.setVisible(true);
			}
			
			public void onFailure(Throwable caught) {
				propositionListGrid.setVisible(false);
				Window.alert(caught.getMessage());					
			}
		});
	}
		
}
